layui.define(['table', 'form', 'element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form, element = layui.element;

  var deviceSn = $('input[name="deviceSn"]').val();

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/bxscn/device/portable_battery/order/list.data'
    ,where:{portableBatterySn:deviceSn}
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
       {field: 'tradeNo', title: '订单号'}
      ,{field: 'leaseChargingCabinetSn', title: '租借机柜',templet:function (record){
          if(record.leaseChargingCabinetSn) {
              return record.leaseChargingCabinetSn + '(卡槽：' + record.leaseSlotNum + ')';
          }else{
              return '--';
          }
      }}
      ,{field: 'leaseAreaName', title: '租借地区'}
      ,{field: 'leasePower', title: '租借电量'}
      ,{field: 'revertChargingCabinetSn', title: '归还机柜',templet:function (record){
          if(record.revertChargingCabinetSn) {
              return record.revertChargingCabinetSn + '(卡槽：' + record.revertSlotNum + ')';
          }else{
              return '--';
          }
      }}
      ,{field: 'revertAreaName', title: '归还地区'}
      ,{field: 'revertPower', title: '归还电量'}
      ,{field: 'useDurationStr', title: '使用时长'}
      ,{field: 'stateName', title: '订单状态'}
      ,{field: 'createDate', title: '创建时间'}
	  ,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
	,limit: 30
    ,text: '对不起，加载出现异常！'
  });
  
  table.render({
      elem: '#LAY-shop-table'
      ,url: layui.setter.webRoot + '/bxscn/device/portable_battery/shop_order/list.data'
	  ,where:{portableBatterySn:deviceSn}
      ,method: 'post'
      ,parseData: function (res) {
          return {
              "code": res.code,
              "msg": res.msg,
              "count": res.body.total,
              "data": res.body.data
          };
      }
      ,response: {statusCode: 200}
      ,cols: [[
        {field: 'tradeNo', title: '订单号'}
        ,{field: 'batteryTypeName', title: '电池类型'}
        ,{field: 'portableBatterySn', title: '电池ID'}
        ,{field: 'leaseTypeName', title: '租借方式'}
        ,{field: 'leasePlaceName', title: '租借网点'}
        ,{field: 'revertTypeName', title: '归还方式'}
        ,{field: 'revertPlaceName', title: '归还网点'}
        ,{field: 'moneyStr', title: '订单金额'}
        ,{field: 'depositMoneyStr', title: '押金'}
        ,{field: 'useDay', title: '使用天数'}
        ,{field: 'payStateName', title: '支付状态',templet:function (record){
            if(record.payState == 4){
                return '<span class="layui-badge">'+record.payStateName+'</span>';
            }else{
                return record.payStateName;
            }
        }}
        ,{field: 'stateName', title: '状态',templet:function (record){
            if(record.state == 2){
                return '<span class="layui-badge layui-bg-green">'+record.stateName+'</span>';
            }else if(record.state == 3 || record.state == 7){
                return '<span class="layui-badge layui-bg-orange">'+record.stateName+'</span>';
            }else{
                return record.stateName;
            }
        }}
        ,{field: 'createDate', title: '创建时间'}
		,{title: '操作', width: 80, align:'center', fixed: 'right', toolbar: '#LAY-shop-table-toolbar'}
      ]]
      ,page: true
      ,limit: 30
      ,text: '对不起，加载出现异常！'
  });
  
  element.on('tab(tab-info)', function (data) {
      if(data.index == 0) {
          table.reload('LAY-table')
      }else if(data.index == 1) {
          table.reload('LAY-shop-table')
      }
  });
  
  //事件
  var active = {
      changeLeaseType:function(){
          layer.confirm('确定要切换租借方式吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/portable_battery/lease_type/change.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          location.reload();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
	  ,updateState:function(){
		 layer.open({
              type: 2
              ,title: '修改电池状态'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/state/update.html?deviceSn='+deviceSn
              ,maxmin: false
              ,area: ['400px', '500px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/state/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  location.reload();
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
	  }
      ,allocationPlace:function (){
          layer.open({
              type: 2
              ,title: '分配电池到网点'
              ,content: layui.setter.webRoot + '/bxscn/device/portable_battery/place/allocation.html?deviceSn='+deviceSn
              ,maxmin: true
              ,area: ['500px', '600px']
              ,btn: ['确定', '取消']
              ,yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/bxscn/device/portable_battery/place/allocation.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  location.reload();
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,undeploy:function(){
          layer.confirm('确定从网点中移除电池吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/bxscn/device/portable_battery/undeploy.do',
                  data:{deviceSn:deviceSn},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          location.reload();
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,orderInfo: function(obj){
          layer.open({
              type: 2
              ,title: '查看订单'
              ,content: layui.setter.webRoot + '/bxscn/order/info.html?tradeNo='+obj.data.tradeNo+'&createDate='+obj.data.createDate
              ,maxmin: true
              ,area: ['80%', '80%']
          });
      }
	  ,shopOrderInfo: function(obj){
	        parent.layer.open({
	            type: 2
	            ,title: '查看订单'
	            ,content: layui.setter.webRoot + '/bxscn/shop_order/info.html?tradeNo='+obj.data.tradeNo+'&createDate='+obj.data.createDate
	            ,maxmin: true
	            ,area: ['80%', '80%']
	        });
	    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  table.on('tool(LAY-shop-table)', function(obj){
	var type = obj.event ;
    active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('portable_energy_battery_info', {})
});